import Vue from 'vue'
import Vuex from 'vuex'

import { setItem, getItem } from '@/utils/storage'

Vue.use(Vuex)

const HeadlineKey = 'headlineKey'
const ChannelsKey = 'channelsKey'

const searchHisKey = 'SearchHisKey'

export default new Vuex.Store({
  state: {
    // 从本地读取数据
    // user: JSON.parse(localStorage.getItem(HeadlineKey))
    user: getItem(HeadlineKey),
    channelsKey: ChannelsKey,
    searchHisKey: searchHisKey
  },
  mutations: {
    setUser (state, data) {
      state.user = data

      // 将数据本地持久化
      // localStorage.setItem(HeadlineKey, JSON.stringify(state.user))
      setItem(HeadlineKey, state.user)
    },

    // 刷新token
    setNewToken (state, newToken) {
      state.user.token = newToken
      setItem(HeadlineKey, state.user)
    }
  },
  actions: {},
  modules: {}
})
